﻿using Game.NetWork;
using Game.NetWork.Packages;
using Game.Server;

namespace Game.Plugin.MyShopPlugin
{
    public class MyShopCmd : AbstractProcessCmd
    {
        public override string Cmd => "money";
        public override string Introduce => "/money    -我的商店相关的命令";
        public override int AuthLevel => 0;
        public override DisplayType Display => DisplayType.All;

        public void SendMyShopHelpMessage()
        {
            string message = "\n/money me    -  查看我的金币余额\n/money give <玩家名字> <金额>    - 给与指定玩家对应金额的钱\n<<<======【我的商店】MyShop指令系统======>>>";
            SendMessage("我的商店", message);
        }

        public void SendMsgPack(ComponentPlayer componentPlayer, string text)
        {
            MessagePackage messagePackage = new MessagePackage(null, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }

        public override void ProcessCmd()
        {
            if (GameManager.Project == null)
            {
                SendMessage("<c=red>主系统</c>", "你需要先开始游戏");
                return;
            }

            if (m_isTerminal)
            {
                SendMessage("<c=red>主系统</c>", "控制台无法使用");
                return;
            }

            switch (m_messageDatas.Length)
            {
                default:
                    SendMyShopHelpMessage();
                    break;
                case 4:
                    {
                        if (!(m_messageDatas[1] == "give"))
                        {
                            break;
                        }

                        // 使用GUID获取玩家数据
                        string playerGuid = m_client.GUID.ToString();
                        MyShopListData playerData = MyShopData.GetDataByGuid(playerGuid);
                        if (playerData == null)
                        {
                            SendMessage("<c=red>主系统</c>", "玩家数据不存在");
                            break;
                        }

                        double playerMoney2 = playerData.playerMoney;

                        // 查找目标玩家
                        PlayerData targetPlayer = null;
                        foreach (PlayerData playersDatum in GameManager.Project.FindSubsystem<SubsystemPlayers>().PlayersData)
                        {
                            if (playersDatum.Name == m_messageDatas[2])
                            {
                                targetPlayer = playersDatum;
                                break;
                            }
                        }

                        if (targetPlayer == null)
                        {
                            SendMessage("<c=red>主系统</c>", "玩家 <c=green>" + m_messageDatas[2] + "</c> 不存在或者不在线");
                            break;
                        }

                        if (targetPlayer.Client.GUID.ToString() == playerGuid)
                        {
                            SendMessage("<c=red>主系统</c>", "你不能给你自己转账");
                            break;
                        }

                        // 使用GUID检查目标玩家数据
                        MyShopListData targetPlayerData = MyShopData.GetDataByGuid(targetPlayer.Client.GUID.ToString());
                        if (targetPlayerData == null)
                        {
                            SendMessage("<c=red>主系统</c>", "玩家 <c=green>" + m_messageDatas[2] + "</c> 在系统里没有任何数据");
                            break;
                        }

                        if (double.TryParse(m_messageDatas[3], out var result) && result > 0.0)
                        {
                            if (playerMoney2 - result >= 0.0)
                            {
                                // 更新转账双方数据
                                MyShopListData newTargetData = new MyShopListData
                                {
                                    playerMoney = targetPlayerData.playerMoney + result,
                                    playerGuid = targetPlayerData.playerGuid,
                                    playerName = targetPlayerData.playerName,
                                    lastSignInTime = targetPlayerData.lastSignInTime
                                };
                                MyShopData.SetMyShopListDataByGuid(targetPlayer.Client.GUID.ToString(), newTargetData);

                                MyShopListData newSourceData = new MyShopListData
                                {
                                    playerMoney = playerMoney2 - result,
                                    playerGuid = playerData.playerGuid,
                                    playerName = playerData.playerName,
                                    lastSignInTime = playerData.lastSignInTime
                                };
                                MyShopData.SetMyShopListDataByGuid(playerGuid, newSourceData);

                                SendMessage("<c=red>主系统</c>", "你给 <c=green>" + m_messageDatas[2] + "</c>玩家 <c=green>" + result + "</c> 个金币");
                                string text = "玩家 <c=green>" + m_client.PlayerData.Name + "</c> 给你转账 <c=green>" + result + "</c> 金币";
                                SendMsgPack(targetPlayer.ComponentPlayer, text);
                            }
                            else
                            {
                                SendMessage("<c=red>主系统</c>", "你的金钱不够");
                            }
                        }
                        else
                        {
                            SendMessage("<c=red>主系统</c>", "你输入的金额有误，请重新输入");
                        }
                        break;
                    }
                case 2:
                    if (m_messageDatas[1] == "me")
                    {
                        // 使用GUID获取玩家数据
                        string playerGuid = m_client.GUID.ToString();
                        MyShopListData playerData = MyShopData.GetDataByGuid(playerGuid);
                        if (playerData != null)
                        {
                            SendMessage("<c=red>主系统</c>", "你当前的余额为 <c=green>" + playerData.playerMoney + "</c> 金币");
                        }
                        else
                        {
                            SendMessage("<c=red>主系统</c>", "玩家数据不存在");
                        }
                    }
                    break;
            }
        }
    }
}